package com.smart.manager.wiki.entities.repository;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.Opt;
import com.smart.common.entities.constant.ArcoTips;
import com.smart.common.entities.core.BaseSearch;
import com.smart.common.entities.core.Page;
import com.smart.manager.wiki.entities.body.RIntelNotice;
import com.smart.manager.wiki.entities.enums.NoticeScope;
import com.smart.manager.wiki.entities.models.WikiIntelNotice;

import com.smart.manager.wiki.entities.models.WikiIntelReader;
import com.smart.manager.wiki.entities.models.query.QWikiIntelNotice;
import com.smart.manager.wiki.entities.models.query.QWikiIntelReader;
import com.smart.manager.wiki.entities.enums.IntelType;
import io.ebean.DB;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @author Administrator
 */
@Service
@Log4j2
@RequiredArgsConstructor
public class IntelNoticeRepository {


    public Page<WikiIntelNotice> pageList(RIntelNotice.SearchBody search) {

        QWikiIntelNotice query = new QWikiIntelNotice();
        Opt.ofBlankAble(search.getTitle()).ifPresent(x -> query.title.contains(x));
        query.order().id.desc();
        query.setFirstRow(search.firstRow()).setMaxRows(search.maxRows());
        return new Page<WikiIntelNotice>().load(query.findPagedList());
    }

    public void add(RIntelNotice.EditBody body) {
        BeanUtil.copyProperties(body, WikiIntelNotice.class).setReadCount(0).setReadPerson(0).save();
    }


    public void update(Long id, RIntelNotice.EditBody body) {
        QWikiIntelNotice query = new QWikiIntelNotice();
        WikiIntelNotice record = query.id.eq(id).setMaxRows(1).findOne();
        Assert.notNull(record, "修改失败:通知数据不存在或已被删除");
        BeanUtil.copyProperties(body, record);
        Objects.requireNonNull(record).update();
    }

    public int delByIds(List<Long> ids) {
        List<WikiIntelNotice> list = new QWikiIntelNotice().id.in(ids).findList();
        Assert.notEmpty(list, ArcoTips.DELETE_NOT_FOUND);
        return DB.deleteAll(list);
    }


    public Page<WikiIntelNotice> getPageList(NoticeScope scope, Long memberId, BaseSearch search) {
        QWikiIntelNotice query = new QWikiIntelNotice().scope.eq(scope).order().id.desc();
        query.setFirstRow(search.firstRow()).setMaxRows(search.maxRows());
        Page<WikiIntelNotice> result = new Page<WikiIntelNotice>().load(query.findPagedList());
        List<WikiIntelNotice> records = result.getRecords();
        if (CollUtil.isNotEmpty(records)) {
            List<Long> idList = new QWikiIntelReader().memberId.eq(memberId)
                    .intelType.eq(IntelType.NOTICE)
                    .select(QWikiIntelReader.Alias.intelId)
                    .findSingleAttributeList();
            records.forEach(x -> x.setShowDot(!idList.contains(x.getId())));
        }
        return result;
    }

    public WikiIntelNotice getDetail(Long memberId, Long id) {
        WikiIntelNotice notice = new QWikiIntelNotice().id.eq(id).setMaxRows(1).findOne();
        Assert.notNull(notice, "通知信息不存在");
        addRecord(notice, memberId);
        return notice;
    }


    @Async
    public void addRecord(WikiIntelNotice notice, Long memberId) {
        //增加阅读次数
        Integer readCount = notice.getReadCount();
        Integer readPerson = notice.getReadPerson();
        Long noticeId = notice.getId();
        boolean exists = new QWikiIntelReader().intelId.eq(noticeId).memberId.eq(memberId).setMaxRows(1).exists();
        if (!exists) {
            //增加阅读人数
            notice.setReadPerson(readPerson + 1);
            new WikiIntelReader().setIntelType(IntelType.NOTICE).setIntelId(noticeId).setMemberId(memberId).setReadTime(LocalDateTime.now()).save();
        }
        //增加阅读次数
        notice.setReadCount(readCount + 1).update();
    }

}
